from django.core.management.base import BaseCommand, CommandError
from django_redis import get_redis_connection
from mysqldb import models as MYSQL
import hashlib


def SelectPlaceIDList():
    """查询场所列表"""
    return list(MYSQL.AllinpayPlaceList.objects.all().values_list("id", flat=True))


def initialization():
    print("# [\033[1;33m 初始化数据 \033[0m] ...")
    # 初始化管理员角色
    MYSQL.AllinpayAuthGroup.objects.update_or_create(
        defaults={"pid": 0, "name": "默认角色"}, id=1
    )
    MYSQL.AllinpayAuthGroup.objects.update_or_create(
        defaults={"pid": 0, "name": "管理员"}, id=2
    )

    # 初始化管理员
    MYSQL.AllinpayUserList.objects.update_or_create(
        defaults={
            "name": "超级管理员",
            "phone": "13896370136",
            "status": 1,
            "group": 1,
            "is_active": 1,
            "is_authenticated": 1,
            "is_super": 1,
        },
        id=1,
    )
    # 后台营收
    MYSQL.AllinpayTermList.objects.update_or_create(defaults={"status": 1}, id=1)

    # 初始化场所员工
    MYSQL.AllinpayStaffList.objects.update_or_create(
        defaults={"status": 0, "basename": "测试员工"}, id=1000
    )

    # 初始化场所
    MYSQL.AllinpayPlaceList.objects.update_or_create(
        defaults={"status": 0, "name": "测试场所", "telephone": "13000000000"},
        id=1000000,
    )

    # 初始化会员
    MYSQL.AllinpayMemberList.objects.update_or_create(
        defaults={"pid": 1000000, "name": "测试会员"}, id=1000000000
    )
    MYSQL.BMemberList.objects.update_or_create(
        defaults={"name": "测试会员", "hash": "xxx"}, id=1000000000
    )

    for i in SelectPlaceIDList():
        MYSQL.MemberLevelruleList.objects.update_or_create(
            defaults={"status": 1, "lastreg": 7, "bem": "刚注册7天"}, pid=i, name="新会员"
        )
        MYSQL.MemberLevelruleList.objects.update_or_create(
            defaults={"status": 1, "lastreg": 30, "bem": "刚注册30天"}, pid=i, name="不稳定会员"
        )
        MYSQL.MemberLevelruleList.objects.update_or_create(
            defaults={"status": 1, "week": 3, "bem": "一周到店3次"}, pid=i, name="常来会员"
        )
        MYSQL.MemberLevelruleList.objects.update_or_create(
            defaults={"status": 1, "week": 5, "bem": "一周到店5次"}, pid=i, name="忠实会员"
        )
        MYSQL.MemberLevelruleList.objects.update_or_create(
            defaults={
                "status": 1,
                "lol": 200,
                "csgo": 20000,
                "steam": 20000,
                "bem": "lol指数达到200，Steam指数达到20000，csgo指数达到20000",
            },
            pid=i,
            name="潜在大咖",
        )
        MYSQL.MemberLevelruleList.objects.update_or_create(
            defaults={"status": 1, "other": 100, "bem": "综合排名前100"}, pid=i, name="大咖100"
        )

    # 初始化工作项类型
    MYSQL.AllinpayWorkitenTypes.objects.update_or_create(
        defaults={"name": "员工作业"}, id=1
    )
    MYSQL.AllinpayWorkitenTypes.objects.update_or_create(
        defaults={"name": "检查作业"}, id=2
    )
    MYSQL.AllinpayWorkitenTypes.objects.update_or_create(
        defaults={"name": "保洁作业"}, id=3
    )
    MYSQL.AllinpayWorkitenTypes.objects.update_or_create(
        defaults={"name": "定时作业"}, id=4
    )
    MYSQL.AllinpayWorkitenTypes.objects.update_or_create(
        defaults={"name": "自动作业"}, id=5
    )

    # 初始化积分类型
    MYSQL.AllinpayIntegralList.objects.update_or_create(
        defaults={"name": "现金积分", "status": 1}, id=1
    )
    MYSQL.AllinpayIntegralList.objects.update_or_create(
        defaults={"name": "呼叫积分", "status": 1}, id=2
    )
    MYSQL.AllinpayIntegralList.objects.update_or_create(
        defaults={"name": "会员积分", "status": 1}, id=3
    )
    MYSQL.AllinpayIntegralList.objects.update_or_create(
        defaults={"name": "员工作业分", "status": 1}, id=4
    )
    # print("[\033[1;32m 初始化 \033[0m] iClouds.wsgi.initialize 全局配置...")


def clear_cache(cur=0):
    """清除缓存"""
    print("# [\033[1;33m 清除持续化缓存 \033[0m] ...")
    public = ["workStatus", "loginPlace", "syncAlert", "lizhu", "sems"]
    for i in public:
        rds = get_redis_connection(i)
        d = []
        while True:
            r = rds.scan(cur)
            cur = r[0]
            d += r[1]
            if r[0] == 0:
                break
        for k in d:
            rds.delete(k)


class Command(BaseCommand):
    help = "My custom startup command"

    def handle(self, *args, **kwargs):
        try:
            # put startup code here
            print("# [\033[1;32m 加载初始化数据 \033[0m] ...")
            # 清除缓存
            # clear_cache()
            # 启动脚本
            initialization()
        except:
            raise CommandError("Initalization failed.")
